package globals;

public class Vector2D {
	// All vectors are set at O(0,0)
	public float x,y;
	public Vector2D(){
		x=1;	y=1;
	}
	public Vector2D(float xin, float yin){
		x=xin;	y=yin;
	}
	public Vector2D(Point a, Point b){ // vector from a to b
		x=b.x-a.x;	y=b.y-a.y;
	}
	
	public float getMagnatude(){
		return (float) ( Math.hypot(x, y) );
	}
	public float getAngle(){
		float returnAngle=(float) Math.atan(y/x);
		if(returnAngle<0){returnAngle+=Math.PI;}
		if(y<0){returnAngle+=Math.PI;}
		else if(y==0 && x<0){returnAngle+=Math.PI;}
		
		return ( returnAngle );
	}
	public float getX(){return x;}
	public float getY(){return y;}
	public Vector2D getUnitVector(){
		Vector2D returnVector=this.getCopy();
		returnVector.setMagnatude(1);
		return(returnVector);
	}
	public Vector2D getCopy(){return(new Vector2D(this.getX(), this.getY()));}
	
	public void setPoint(float xin, float yin){x=xin;	y=yin;}
	public void setPoint(float x1in, float y1in, float x2in, float y2in){x=x2in-x1in;	y=y2in-y1in;}
	public void setX(float xin){x=xin;}
	public void setY(float yin){y=yin;}
	public void setAngle(float a){
		float lMag=this.getMagnatude();
		this.setX((float) (lMag*Math.cos(a)));
		this.setY((float) (lMag*Math.sin(a)));
		//this.setPoint((float) Math.cos(a)*this.getMagnatude(),(float) Math.sin(a)*this.getMagnatude());
	}
	public void setMagnatude(float n){
		float lAng=this.getAngle();
		this.setX((float) (n*Math.cos(lAng)));
		this.setY((float) (n*Math.sin(lAng)));
		//this.setPoint((float) Math.cos(this.getAngle())*n,(float) Math.sin(this.getAngle())*n);
	}
	
	
	public void add(Vector2D b){
		x+=b.getX();
		y+=b.getY();
	}
	public void sub(Vector2D b){
		x-=b.getX();
		y-=b.getY();
	}
	public float dot(Vector2D b){
		return( (x*b.getX())+(y*b.getY()) );
	}
	
	public void negate(){
		x=-1*x;
		y=-1*y;
	}
	public Vector2D getOrthoganal(){
		return(new Vector2D(-1*y,x));
	}

}
